<template>
	<view class="myAuth">
	<!-- 	<uni-nav-bar title="新增岗位" left-icon="left" @clickLeft="navBack" backgroundColor="#2CB9A4" color="#F8F8F8"></uni-nav-bar> -->
		<u--form labelPosition="left" :model="formData" :rules="rules" ref="form1">
			<u-form-item label="岗位名称" prop="name" borderBottom ref="item1" labelWidth="100" :required="type!=1">
				<u--input v-model="formData.name" border="none" placeholder="请输入岗位名称" maxlength="11">
				</u--input>
			</u-form-item>
			<u-form-item label="职务范围" prop="scope" borderBottom ref="item1" labelWidth="100" :required="true"
				labelPosition="top">
				<u--textarea v-model="formData.scope" border="none" placeholder="请输入职务范围" type="text">
				</u--textarea>
			</u-form-item>

		</u--form>
		<view class="botBtn">
			<view style="width: 100%;">
				<u-button class="custom-style" text="提交"
					customStyle="background: linear-gradient(90deg, #2CB9A4 0%, #C7EACE 100%);color:#fff;margin-top:20px;border-radius: 30px;"
					@click="goAuth">
				</u-button>
			</view>
		</view>
		<lingfeng-timepicker ref="timePop" type="datetime-range" :minDate="minDate" :maxDate="maxDate"
			@change="timeChange"></lingfeng-timepicker>
	</view>
</template>
<style>
	page {
		background-color: #fff;
	}
</style>
<script>
	import eventBUs from '../../utils/eventBus.js'
	import vvSelect from '@/components/vv-select/vv-select.vue'
	import {
		addPost,
		updatePost
	} from '@/request/api.js'
	export default {
		components: {
			vvSelect,
		},
		data() {
			return {
				comStatus: '商品',
				minDate:{
					year: 2020,
					month: 3,
					date: 5
				},
				maxDate:{
					year: 2033,
					month: 9,
					date: 20
				},
				formData: {
					name: "",
					scope: '',
					id: '',
				},
				rules: {
					'name': {
						type: 'string',
						transform(value) {
							return String(value);
						},
						required: true,
						message: '请输入岗位名称',
					},
					'scope': {
						type: 'string',
						transform(value) {
							return String(value);
						},
						required: true,
						message: '请输入职务范围',
					},
					
				}
			}
		},
		onLoad(res) {
			// let data = JSON.parxse(res.data)
			if (res.data) {
				this.formData = JSON.parse(res.data)
				console.log(this.formData, 11)
			}
		},
		methods: {
			empt() {
				for (let k in this.formData) {
					this.formData[k] = ''
				}
			},
			async updatePost() {
				let res = await updatePost(this.formData)
				uni.navigateTo({
					url: '/my_pages/components/success?title=修改成功'
				})
				this.empt()
			},
			goAuth() {
				console.log(this.formData)
				this.$refs.form1.validate().then(res => {
					if (this.formData.id) {
						this.updatePost()
						return
					}
					addPost(this.formData).then(res => {
						if (res.code == 200) {
							uni.navigateTo({
								url: '/my_pages/components/success?title=添加岗位成功'
							})
							this.empt()
						}
					})
				}).catch(errors => {
					uni.$u.toast('请正确填写信息')
				})
			},
			async updateGoods() {
				let res = await updateGoods(this.formData)
				uni.navigateTo({
					url: '/my_pages/components/success?title=修改成功'
				})
				this.empt()
			}
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.form1.setRules(this.rules)
		},
	}
</script>

<style lang="scss" scoped>
	.comTab {
		display: flex;
	}

	.myAuth {
		padding: 30rpx;
	}

	::v-deep .u-input {
		background-color: #fff !important;
	}

	::v-deep .u-transition {
		overflow: scroll !important;
		height: 50vh !important;
	}

	.compony {
		display: flex;
		padding-top: 6px;
		padding-left: 9px;
		padding-right: 9px;
		height: 24px;
		border-radius: 4px;
		justify-content: space-between;

		.select {
			flex: 1;
		}
	}

	.botBtn {
		display: flex;
		justify-content: center;
		align-items: center;
		position: fixed;
		/* width: 100%; */
		right: 0;
		padding: 20rpx;
		left: 0;
		bottom: 60rpx;
	}

	.custom-style {
		width: 48% !important;
	}
</style>